package com.xixi.demo.mapper;

import com.xixi.demo.model.BlogInfo;
import com.xixi.demo.model.PageResult;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface BlogMapper {
    @Select("select id, title, content, user_id, update_time from blog where delete_flag = 0 order by id desc")
    List<BlogInfo> selectAll();

    @Select("select id, title, content, update_time from blog where user_id = #{userId} and delete_flag = 0")
    BlogInfo selectByUserId(Integer userId);

    @Select("select id, title, content, user_id, update_time from blog where id = #{blogId} and delete_flag = 0")
    BlogInfo selectById(Integer blogId);

    @Insert("insert into blog (title, content, user_id) values (#{title}, #{content}, #{userId})")
    Integer insertBlog(BlogInfo blogInfo);

    Integer updateBlog(BlogInfo blogInfo);

    @Update("update blog set delete_flag = 1 where id = #{blogId}")
    Integer deleteBlog(Integer blogId);

    @Select("select count(1) from blog where delete_flag = 0")
    Integer count();

    @Select("select id, title, content, user_id, update_time from blog where delete_flag = 0 order by id desc limit #{offset}, #{pageSize}")
    List<BlogInfo> getBookListByPage(Integer offset, Integer pageSize);
}
